package com.wj.graph;

import com.wj.linear.Queue;

/**
 * @author wen.jie
 * @date 2021/8/30 9:33
 */
public class EdgeWeightedGraph {

    private final int V;

    private int E;

    private Queue<Edge>[] adj;


    public EdgeWeightedGraph(int V) {
        this.V = V;
        this.E = 0;
        this.adj = new Queue[V];

        for (int i = 0; i < adj.length; i++) {
            adj[i] = new Queue<>();
        }
    }

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }

    public void addEdge(Edge e) {
        int v = e.either();
        int w = e.other(v);
        adj[v].enqueue(e);
        adj[w].enqueue(e);
        E++;
    }

    public Queue<Edge> adj(int v) {
        return adj[v];
    }

    /**
     * @author wen.jie
     * @date 2021/8/30 9:40
     * 获取加权无向图中的所有边
     */
    public Queue<Edge> edges(){
        Queue<Edge> allEdges = new Queue<>();
        for (int v = 0; v < V; v++) {
            //遍历v顶点的邻接表
            for (Edge e : adj(v)) {
                if (e.other(v) < v)
                    allEdges.enqueue(e);
            }
        }
        return allEdges;
    }
}
